//
//  Push2Strategy.m
//  HYHOME
//
//  Created by cjy on 16/3/3.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "Push2Strategy.h"
#import "PushForStrategyCell.h"
#import "NetWorkHandler.h"
#import "ModelS.h"
#import "DKNightVersion.h"
@interface Push2Strategy ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableView;

@property (nonatomic,retain)NSMutableArray *arr;
@property (nonatomic,retain)NSString *strpage;
@end

@implementation Push2Strategy

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    
    self.strpage = [ NSString stringWithFormat:@"{\"user_id\":0,\"article_id\":\"%ld\",\"device_id\":\"311FB630-A637-4803-95BA-2AEC74486504\"}",(long)self.ida];
    [self handle];
    [self createtableView];
    
}

- (void)handle{
    self.arr = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:@"http://tuku-wap.m.jia.com/v1.2.4/hybrid/article/find"];


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    request.HTTPMethod= @"POST";

    request.HTTPBody = [self.strpage dataUsingEncoding:NSUTF8StringEncoding];

    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    
    // 进入主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        
        NSError *errorJSON = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorJSON];
        
       
        NSArray *array = [result objectForKey:@"article_info_list"];
        
        
        for (NSDictionary *dic in array) {
            
   
                
                ModelS *model = [[ModelS alloc]init];
            
            
            
                model.main_body = [dic objectForKey:@"main_body"];
                
                
                NSDictionary *dicI = [dic objectForKey:@"image"];
      
                
       
         
                model.url = [dicI objectForKey:@"url"];
            
            
                model.ida = [result objectForKey:@"id"];
            
                
                [self.arr addObject:model];
                

                
            }
            
        
            
            
        
        
        
        [self.tableView reloadData];
    
    });
    
    
    
    
}];


    [task resume];


}



- (void)createtableView{

    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[PushForStrategyCell class] forCellReuseIdentifier:@"pool"];

    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);



}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return self.arr.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    PushForStrategyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    ModelS *model = [self.arr objectAtIndex:indexPath.row];
    
  cell.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);

    cell.selectionStyle = UITableViewCellEditingStyleNone;
    [cell passmodel:model];
    return cell;
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [PushForStrategyCell heightForCell:self.arr[indexPath.row]];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
